<%-- Copyright (c) 2010 Canadensys --%>
<%-- JSP to handle Ajax call to create a CSV file --%>
<%-- TODO : combine parameters handling with dwc.jsp --%>
<%@ page import="java.io.BufferedWriter" %>
<%@ page import="java.io.FileOutputStream" %>
<%@ page import="java.io.OutputStreamWriter" %>
<%@ page import="java.util.UUID" %>
<%@ page import="net.canadensys.dataportal.vascan.downloads.TextFileManager" %>
<%@ page import="net.canadensys.dataportal.vascan.config.impl.InstallationConfig" %>
<%@ include file="../_inc/functions.jsp" %>
<%
	try{
	out.clear();
    // archive file name;
    String filename = "";
    if(session.getAttribute("filename") != null){
        filename = (String)session.getAttribute("filename");
    }
    else{
    	//here we should generate a new name and send it back
    	System.out.println("Session id:" + session.getId() + " is empty");
    }

    /* request params */
    /* provinces */
    String[] province = null;
    if(request.getParameterValues("province") != null)
     province = request.getParameterValues("province");
    
    /* combination */
    String combination = "";
    if(request.getParameterValues("combination") != null)
        combination = request.getParameter("combination");  
    
    /* habitus */
    String habit = "";
    if(request.getParameterValues("habit") != null)
        habit = request.getParameter("habit");
    
    /* taxonid */
    int taxon = -1;
    String taxonIdStr = "";
    if(request.getParameterValues("taxon") != null){
    	taxonIdStr = request.getParameter("taxon");
        if(!taxonIdStr.equals(""))
            taxon = Integer.valueOf(taxonIdStr);
    }
    
    /* distribution */
    String[] distributionStatus = null;
    if(request.getParameterValues("status") != null)
    	distributionStatus = request.getParameterValues("status");

    /* rank */
    String[] rank = null;
    if(request.getParameterValues("rank") != null)
        rank = request.getParameterValues("rank");
    
    /* display hybrids */
    String nohybrids = null;
    if(request.getParameter("nohybrids") != null)
        nohybrids = request.getParameter("nohybrids");
    String hybrids = null;
    if(request.getParameter("hybrids") != null)
        hybrids = request.getParameter("hybrids");
    
    
    /* sort */
    String sort = null;
    if(request.getParameter("sort") != null)
        sort = request.getParameter("sort");

    if(distributionStatus == null){
        String statuses[] = {"introduced","native","ephemeral","excluded","extirpated","doubtful"};
        distributionStatus = statuses;
    }
    
    if(habit == "")
    	habit = "all";
    
    if(combination == "")
        combination = "anyof";
    
    // sort options
    if(sort == "" && sort == null){
    	sort = "taxonomically";
    }

    // hybrids checkbox
    if((nohybrids == null || nohybrids == "") && (hybrids == null || hybrids == "")){
        hybrids = "true";
    }
    else{
        if((nohybrids != "" && nohybrids != null) && (hybrids == null || hybrids == "")){
               hybrids = null;         
        }
        else{
            hybrids = "true";   
        }
    }
    
    String destinationFilePath = InstallationConfig.getInstance().get(InstallationConfig.GENERATED_CONTENT_DIR_KEY) + filename;
    TextFileManager csvManager = new TextFileManager(_bundle);
    boolean success = csvManager.generateCSV(destinationFilePath, province, combination, habit, taxon, distributionStatus, rank, hybrids, sort);
    
    //we should return a JSON string and include the file name in case the filename was not available in the session.
    if(success){
    	out.write("success");
    }
    else{
    	out.write("error");
    }
}
catch(Exception e){
    e.printStackTrace();
    out.write(e.toString());
    out.write("error");
}
%>